package com.zwx.linkList.leetcode_4;

import com.zwx.linkList.leetcode_3.DeleteNode;

/**
 * 反转链表
 * <p>
 * 输入: 1->2->3->4->5->NULL
 * 输出: 5->4->3->2->1->NULL
 *
 * @author 文希
 * @create 2019-07-03 23:01
 */
public class ReverseList {
    
    private static ListNode reverserList(ListNode head){
        ListNode pre = null;
        while (head != null){
            ListNode temp = head.next;
            // 当前节点的后一节点 = 前一节点
            // 同时因为前一节点总是首节点
            head.next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode listNode5 = new ListNode(5,null);
        ListNode listNode4 = new ListNode(4,listNode5);
        ListNode listNode3 = new ListNode(3,listNode4);
        ListNode listNode2 = new ListNode(2,listNode3);
        ListNode listNode1 = new ListNode(1,listNode2);

        ListNode node = reverserList(listNode1);
        System.out.println("node = " + node);
    }

    static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }
}
